package com.jml.集群;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.List;

public class ProducerSend {
    // 创建我们的分组
    DefaultMQProducer producer = new DefaultMQProducer("mayiktGroup");

    public void sendMsg() throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        // 设置我们的NamesrvAddr
        producer.setNamesrvAddr("192.168.212.169:9876");
        producer.start();
        // 发送我们的消息
        String msg = "jml";
        Long orderId = 123456L;
        Message msg1 = new Message("mtTopic", msg.getBytes());
        SendResult result = producer.send(msg1, new MessageQueueSelector() {
            public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
                // 设置该消息永远存放到 0的位置中。
                return mqs.get(0);
            }
        }, orderId);
        System.out.println("生产者获取msgId:" + result.getMsgId() + ",:" + result.getMessageQueue());
    }

    public static void main(String[] args) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
        ProducerSend producerSend = new ProducerSend();
        producerSend.sendMsg();
    }
}

